//
//  ViewController.m
//  Wangi
//
//  Created by ChuXiang on 15/11/23.
//  Copyright © 2015年 ChuXiang. All rights reserved.
//

#import "ViewController.h"
#import "BaseModel.h"
#import "BaseCollectionViewCell.h"
#import "NetWorkTaskHandle.h"
#import "ReadingViewController.h"
#import "ContentViewController.h"

#define kWidth (self.view.bounds.size.width) / 5

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate, model>
@property (nonatomic, retain) NSMutableArray *newsData;
@property (nonatomic, retain) NSMutableArray *amusementData;
@property (nonatomic, retain) NSMutableArray *scienceData;
@property (nonatomic, retain) NSMutableArray *sportData;
@property (nonatomic, retain) NSMutableArray *finaceData;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UIButton *headlineButton;
@property (nonatomic, retain) UIButton *amusementButton;
@property (nonatomic, retain) UIButton *scienceButton;
@property (nonatomic, retain) UIButton *sportButton;
@property (nonatomic, retain) UIButton *financeButton;

@end

@implementation ViewController
- (void)dealloc {
    [super dealloc];
    [_collection release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title =  @"網易";
    self.navigationController.navigationBar.translucent = NO;
    _newsData = [[NSMutableArray alloc] initWithCapacity:0];
    [self getDataWithNumber:@"T1348647853363" withURL:kNewsGetURL withData:_newsData];
    _amusementData = [[NSMutableArray alloc] initWithCapacity:0];
    [self getDataWithNumber:@"T1348648517839" withURL:kAmusementGetURL withData:_amusementData];
    _scienceData = [[NSMutableArray alloc]initWithCapacity:0];
    [self getDataWithNumber:@"T1348649580692" withURL:kDaLianGetURL withData:_scienceData];
    _sportData = [[NSMutableArray alloc] initWithCapacity:0];
    [self getDataWithNumber:@"T1348649079062" withURL:kSportGetURL withData:_sportData];
    _finaceData = [[NSMutableArray alloc] initWithCapacity:0];
    [self getDataWithNumber:@"T1348648756099" withURL:kFinanceGetURL withData:_finaceData];

    [self.view addSubview:self.collection];
    [self.view addSubview:self.headlineButton];
    [self.view addSubview:self.amusementButton];
    [self.view addSubview:self.scienceButton];
    [self.view addSubview:self.sportButton];
    [self.view addSubview:self.financeButton];
    [_headlineButton release];
    [_amusementButton release];
    [_scienceButton release];
    [_sportButton release];
    [_financeButton release];
    [_collection release];
}
#pragma mark - 协议
- (void)setModel:(BaseModel *)model {
    ContentViewController *centView = [[ContentViewController alloc] init];
    centView.model = model;
    [self.navigationController pushViewController:centView animated:YES];
}

#pragma mark - ScrllView Void
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    _headlineButton.tintColor = [UIColor blackColor];
    _headlineButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _amusementButton.tintColor = [UIColor blackColor];
    _amusementButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _scienceButton.tintColor = [UIColor blackColor];
    _scienceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _sportButton.tintColor = [UIColor blackColor];
    _sportButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _financeButton.tintColor = [UIColor blackColor];
    _financeButton.titleLabel.font = [UIFont systemFontOfSize:15];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        _headlineButton.tintColor = [UIColor redColor];
        _headlineButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    if (scrollView.contentOffset.x == self.view.width) {
        _amusementButton.tintColor = [UIColor redColor];
        _amusementButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    if (scrollView.contentOffset.x == self.view.width * 2) {
        _scienceButton.tintColor = [UIColor redColor];
        _scienceButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    if (scrollView.contentOffset.x == self.view.width * 3) {
        _sportButton.tintColor = [UIColor redColor];
            _sportButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    if (scrollView.contentOffset.x == self.view.width * 4) {
        _financeButton.tintColor = [UIColor redColor];
        _financeButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
}

#pragma mark - GetData
- (void)getDataWithNumber:(NSString *)num withURL:(NSString *)url withData:(NSMutableArray *)data{
//    [SANetWorkingTask request:url parmater:nil block:^(id result) {
//        if (result) {
//            NSArray *array = result[num];
//            for (NSDictionary *dic in array) {
//                BaseModel *model = [[BaseModel alloc] initWithData:dic];
//                [data addObject:model];
//            }
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.collection reloadData];
//            });
//
//        }
//}];
//    
    [NetWorkTaskHandle sendTask:url withResult:^(id result) {
        if (result) {
            NSArray *array = result[num];
            for (NSDictionary *dic in array) {
                BaseModel *model = [[BaseModel alloc] initWithData:dic];
                [data addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                            [self.collection reloadData];
            });
        }
    }];

}




#pragma mark - private
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"BaseCollectionViewCellIdentifier" forIndexPath:indexPath];
    item.delegate = self;
    if (indexPath.item == 0) {
        item.data = self.newsData;
    }
    if (indexPath.item == 1) {
        item.data = self.amusementData;
    }
    if (indexPath.item == 2) {
        item.data = self.scienceData;
    }
    if (indexPath.item == 3) {
        item.data = self.sportData;
    }
    if (indexPath.item == 4) {
        item.data = self.finaceData;
    }
    return item;
}

#pragma mark - ButtonDidPrsee
- (void)headlineButtonDidPress:(UIButton *)sender{
    sender.tintColor = [UIColor redColor];
    sender.titleLabel.font = [UIFont systemFontOfSize:20];
    _amusementButton.tintColor = [UIColor blackColor];
    _amusementButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _scienceButton.tintColor = [UIColor blackColor];
    _scienceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _sportButton.tintColor = [UIColor blackColor];
    _sportButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _financeButton.tintColor = [UIColor blackColor];
    _financeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)amusementButtonDidPress:(UIButton *)sender {
      sender.tintColor = [UIColor redColor];
      sender.titleLabel.font = [UIFont systemFontOfSize:20];
    _headlineButton.tintColor = [UIColor blackColor];
    _headlineButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _scienceButton.tintColor = [UIColor blackColor];
    _scienceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _sportButton.tintColor = [UIColor blackColor];
    _sportButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _financeButton.tintColor = [UIColor blackColor];
    _financeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
    [_collection setContentOffset:CGPointMake(self.view.width, 0) animated:YES];
}

- (void)scienceButtonDidPress:(UIButton *)sender {
    sender.tintColor = [UIColor redColor];
    sender.titleLabel.font = [UIFont systemFontOfSize:20];
    _headlineButton.tintColor = [UIColor blackColor];
    _headlineButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _amusementButton.tintColor = [UIColor blackColor];
    _amusementButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _sportButton.tintColor = [UIColor blackColor];
    _sportButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _financeButton.tintColor = [UIColor blackColor];
    _financeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
    [_collection setContentOffset:CGPointMake(self.view.width * 2, 0) animated:YES];
}

- (void)sportButtonDidPress:(UIButton *)sender {
    sender.tintColor = [UIColor redColor];
    sender.titleLabel.font = [UIFont systemFontOfSize:20];
    _headlineButton.tintColor = [UIColor blackColor];
    _headlineButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _amusementButton.tintColor = [UIColor blackColor];
    _amusementButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _scienceButton.tintColor = [UIColor blackColor];
    _scienceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _financeButton.tintColor = [UIColor blackColor];
    _financeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
    [_collection setContentOffset:CGPointMake(self.view.width * 3, 0) animated:YES];
}

- (void)finaceButtonDidPress:(UIButton *)sender {
    sender.tintColor = [UIColor redColor];
    sender.titleLabel.font = [UIFont systemFontOfSize:20];
    _headlineButton.tintColor = [UIColor blackColor];
    _headlineButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _amusementButton.tintColor = [UIColor blackColor];
    _amusementButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _scienceButton.tintColor = [UIColor blackColor];
    _scienceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _sportButton.tintColor = [UIColor blackColor];
    _sportButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_collection setContentOffset:CGPointMake(0, 0) animated:YES];
    [_collection setContentOffset:CGPointMake(self.view.width * 4, 0) animated:YES];
}

#pragma mark - 懒加载
- (UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(self.view.width , self.view.height - 138);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 0;
        self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) collectionViewLayout:flowLayout];
        _collection.dataSource = self;
        _collection.delegate = self;
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.pagingEnabled = YES;
        [_collection registerClass:[BaseCollectionViewCell class] forCellWithReuseIdentifier:@"BaseCollectionViewCellIdentifier"];
        [_collection release];
    }
    return _collection;
}

- (UIButton *)headlineButton {
    if (!_headlineButton) {
        self.headlineButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_headlineButton setTitle:@"头条" forState:UIControlStateNormal];
        _headlineButton.frame = CGRectMake(0, 10, kWidth, 20);
        
        _headlineButton.tintColor = [UIColor redColor];
        _headlineButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_headlineButton addTarget:self action:@selector(headlineButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headlineButton;
}

- (UIButton *)amusementButton {
    if (!_amusementButton) {
        self.amusementButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_amusementButton setTitle:@"娱乐" forState:UIControlStateNormal];
        _amusementButton.frame = CGRectMake(kWidth, 10, kWidth, 20);
        _amusementButton.tintColor = [UIColor blackColor];
        [_amusementButton addTarget:self action:@selector(amusementButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _amusementButton;
}

- (UIButton *)scienceButton {
    if (!_scienceButton) {
        self.scienceButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_scienceButton setTitle:@"科技" forState:UIControlStateNormal];
        _scienceButton.frame = CGRectMake(kWidth * 2, 10, kWidth, 20);
        _scienceButton.tintColor = [UIColor blackColor];
        [_scienceButton addTarget:self action:@selector(scienceButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _scienceButton;
}

- (UIButton *)sportButton {
    if (!_sportButton) {
        self.sportButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_sportButton setTitle:@"体育" forState:UIControlStateNormal];
        _sportButton.frame = CGRectMake(kWidth * 3, 10, kWidth, 20);
        _sportButton.tintColor = [UIColor blackColor];
        [_sportButton addTarget:self action:@selector(sportButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sportButton;
}

- (UIButton *) financeButton {
    if (!_financeButton) {
        self.financeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_financeButton setTitle:@"财经" forState:UIControlStateNormal];
        _financeButton.frame = CGRectMake(kWidth * 4, 10, kWidth, 20);
        _financeButton.tintColor = [UIColor blackColor];
        [_financeButton addTarget:self action:@selector(finaceButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _financeButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
